﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItem.h"
#include "GnufInfo.h"
#include "GnufToolButton.h"
#include "GnufDropDownBoard.h"

/// \file GnufTagPad.h
namespace gnuf {
    class Tag;
    class QtTagPad;
    class DropDownBoard;

    /// \brief 标签面板
    /// \see IDropDownBoardObserver
    class GNUF_BASE_EXPORT TagPad final : public ToolButton
    {
        friend class TableRow;
        CANNOT_COPY_OR_ASSIGN(TagPad)
    public:
        static const EItemType Type;
        typedef typename std::list<gcmp::OwnerPtr<Tag>> Tags;
        typedef typename std::vector<Tag*> RawTags;
        TagPad(Item* parent);
        ~TagPad();

        /// \brief 往TagPad中添加Tag
        /// \details 添加的Tag默认处于非激活状态，是不可见的
        /// \param info 设置Tag的Info
        /// \see Tag
        void addItem(const Info& info);

        /// \brief 根据Tag的标题从TagPad上删除Tag
        /// \param tagCaption Tag的标题
        void deleteItem(const std::wstring& tagCaption);

        /// \brief 设置Tag的选中状态
        /// \details 选中的Tag会变成可见状态，可以再次关闭进入非选中状态
        /// \param tagCaption Tag的标题
        /// \param bActivated 状态 为true，Tag被选中，为false，Tag未选中状态，隐藏
        void selectItem(const std::wstring& tagCaption, bool bSelected, bool wantNotify = false);

        void selectItem(Tag* pTag, bool bSelected, bool wantNotify = false);

        /// \brief 获取所有的Tag
        const Tags& tags() const { return m_allItem; };
        RawTags rawTags();

        /// \brief 获取所有Tag的Caption
        const std::vector<std::wstring> allCaptions() const;

        /// \brief 获取所有激活的Tag
        RawTags selectedTags();

        /// \brief 获取所有激活的Tag的Caption
        const std::vector<std::wstring> selectedCaptions() const;

        /// \brief 设置无激活Tag的时候，默认显示的文字
        void setTextWhenNoneTagSelected(const std::wstring& text);
        const std::wstring& textWhenNoneTagSelected() const;

        bool shouldShowToolTips(Item* pItem) const;

        virtual void onChildHeightChanged(Item* child, int heignt);
    protected:
        virtual void onDisconnectFromMyQtItem() override;
        virtual void onPostAbilityChanged() override;
        virtual void onPostAppearanceSettingChanged() override;
        virtual void onPostInfoChanged() override;
        virtual void onPostVisibilityChanged() override;
        virtual void onTableRowSelectionChanged(bool selected) override;

        virtual void onDropDownBoardItemClicked(Item* item) override;

    private:
        // 更新到DropDownBoard的内容
        void updateSelectionToDropDownBoard(const std::wstring& tagCaption, bool checked);
        // 更新Pad上标签的内容
        void changeTagSelection(Tag* pTag, bool bSelected, bool wantNotify = true);
        // 设置DropDown的自定义样式
        void setCustomAppearanceSetting();
        // 获取TagPad的高度， 以便更新parentItem的高度
        int getHeight()const ;

        Tags::iterator findTag(const std::wstring& tagCaption);
        Tags::const_iterator findTag(const std::wstring& tagCaption) const;
    private:
        Tags m_allItem;
    };
}
